// services
import APIService from './api.service';
import qs from 'qs';

import type { ResponseData } from './types';

export type LoginInfo = {
  username: string;
  password: string;
  rememberMe: boolean;
};

export type ResetPassParams = {
  username: string;
  mobile: string;
  newPassword: boolean;
};

const API_BASE_URL = import.meta.env.VITE_APP_BASE_URL;

/**
 * @description: 用户api
 * @return {*}
 */
class UserService extends APIService {
  constructor() {
    super(API_BASE_URL);
  }

  async login(params: LoginInfo): Promise<ResponseData<any>> {
    return this.post(`/web/web/user/login?${qs.stringify(params)}`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async registry(params: any): Promise<ResponseData<any>> {
    return this.post(`/web/web/user/register`, params)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async resetPassword(params: ResetPassParams): Promise<any> {
    return this.post('/web/web/user/resetPassword', params)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  async getUserInfo(): Promise<ResponseData<any>> {
    return this.get(`/web/web/user/getUserInfo`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  /**
   * @description: 查询我的邀请，邀请码，佣金，邀请人数，确认中的佣金
   * @return {*}
   */
  async getRecommend(): Promise<ResponseData<any>> {
    return this.get(`/web/web/user/getMyRecommend`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }

  /**
   * @description: 查询我的邀请记录列表
   * @return {*}
   */
  async listCommissionRecord(): Promise<ResponseData<any>> {
    return this.get(`/web/web/user/listMyCommissionRecord`)
      .then((response) => response?.data)
      .catch((error) => {
        throw error?.response?.data;
      });
  }
}

export default new UserService();
